package com.dense.kuiniu.shelfcard.business;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.dense.kuiniu.shelfcard.R;
import com.dense.kuiniu.shelfcard.base.BaseFragment;
import com.dense.kuiniu.shelfcard.card.idcard.IDCardInfo;
import com.dense.kuiniu.shelfcard.utils.timer.CountDownView;
import com.dense.kuiniu.shelfcard.utils.timer.CounterController;

import androidx.annotation.Nullable;
import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author Administrator
 * @date 2020/8/31
 */
public class CardInfoNextDelegate extends BaseFragment {
    private static final String KEY = "info";

    public static CardInfoNextDelegate getInstance(IDCardInfo info) {
        Bundle bundle = new Bundle();
        bundle.putParcelable(KEY, info);
        CardInfoNextDelegate delegate = new CardInfoNextDelegate();
        delegate.setArguments(bundle);
        return delegate;
    }

    @BindView(R.id.tv_title)
    TextView mTvTitle;
    @BindView(R.id.iv_head)
    ImageView mImageView;
    @BindView(R.id.tv_name)
    TextView mTvName;
    @BindView(R.id.tv_sex)
    TextView mTvSex;
    @BindView(R.id.tv_birthday)
    TextView mTvBirthday;
    @BindView(R.id.tv_address)
    TextView mTvAddress;
    @BindView(R.id.count_down_view)
    CountDownView mCountDownView;
    private IDCardInfo mIDCardInfo;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        if (bundle != null) {
            mIDCardInfo = bundle.getParcelable(KEY);
        }
    }

    @Override
    protected int getLayoutRes() {
        return R.layout.delegate_card_info_next;
    }

    @OnClick({R.id.tv_return, R.id.tv_next})
    public void onClick(View view) {
        super.onClick(view);
    }

    @Override
    public void onClick2(View view) {
        int id = view.getId();
        switch (id) {
            case R.id.tv_return:
                exit();
                break;
            case R.id.tv_next:
                next();
                break;
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();

    }

    private void next() {
//        PayChooseDelegate delegate = findFragment(PayChooseDelegate.class);
//        if (delegate == null) {
//            delegate = PayChooseDelegate.getInstance();
//        }
        OutputCardDelegate delegate = findFragment(OutputCardDelegate.class);
        if(delegate == null){
            delegate = OutputCardDelegate.getInstance(mIDCardInfo);
        }
        startWithPop(delegate);
    }

    @Override
    protected void init() {
        try {
            mTvTitle.setText(R.string.text_person_info);
            mTvName.setText(String.format("%s%s", getString(R.string.text_person_name), mIDCardInfo.getName()));
            mTvSex.setText(String.format("%s%s", getString(R.string.text_sex), mIDCardInfo.getSex()));
            mTvBirthday.setText(String.format("%s%s", getString(R.string.text_birthday), mIDCardInfo.getBirthDay()));
            mTvAddress.setText(String.format("%s%s", getString(R.string.text_permanent_address), mIDCardInfo.getAddress()));
            mCountDownView.start(new CounterController.OnCompleteListener() {
                @Override
                public void onComplete() {
                    exit();
                }
            });
            Bitmap bmp = mIDCardInfo.getBitmap();
            if (bmp == null) {
                Resources res = getResources();
                bmp = BitmapFactory.decodeResource(res, R.mipmap.bg_default_head);
            }
            mImageView.setImageBitmap(bmp);
            System.gc();

        } catch (Exception ioe) {
            ioe.printStackTrace();
        }
    }

    private void exit() {
        pop();
    }

}